//
//  HJHomeDetailCell.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/20.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJHomeDetailCell.h"
#import "ORChartView.h"
#import "HJDetailCollectionCell.h"
#import "HJHomeModel.h"

@interface HJHomeDetailCell ()<UICollectionViewDelegate,UICollectionViewDataSource> {
    UIButton * _btn;
    NSInteger index;
    ORChartView * _chartView;
    UICollectionView *  _collectionView;
    NSMutableArray * _modelsArr;
    NSMutableArray * _cellArr;
    BOOL _cellBool;
}
@property(nonatomic,assign) NSIndexPath * cellRow;
@end
@implementation HJHomeDetailCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.clinchadealBtn.titleLabel.font = HJFontSize(mScreenWidth/414*15);
    self.buyBtn.titleLabel.font = HJFontSize(mScreenWidth/414*15);
    self.sellBtn.titleLabel.font = HJFontSize(mScreenWidth/414*15);
    _modelsArr = [NSMutableArray array];
    self.clinchadealBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:15];
    [self configCollection];
    _cellArr = [NSMutableArray array];
    _cellBool =NO;
    self.codeHeight.constant = (mScreenWidth -6*2.5)/5.0 +5;
    
    UIButton *btn = [self viewWithTag:10];
    btn.alpha = 1.0;
    btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:(mScreenWidth/414*15)];
    
    [self loadLineWithdaysArr:nil];
}
-(void)setDaysArr:(NSArray *)daysArr {
    _daysArr = daysArr;
    NSMutableArray * arr = [NSMutableArray array];
    NSMutableArray * dateArr = [NSMutableArray array];
    for (NSDictionary *dic in daysArr) {
        NSString * cost = dic[@"cost"];
        NSString * date = [dic[@"date"] stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
        [arr addObject:cost];
        [dateArr addObject:date];
    }
    _chartView.dataSource = arr;
    _chartView.dataArrOfX = dateArr;
}

-(void)loadLineWithdaysArr:(NSArray *)daysArr {
    if (!self.size) {
        self.size = [mUserDefaults objectForKey:@"often_size"];
    }
    _chartView = [[ORChartView alloc]initWithFrame:CGRectMake(15, 0, mScreenWidth-30, 200) dataSource:nil countFoyY:5];
    _chartView.isMatrix = YES;
    _chartView.isBrokenLine = YES;
    _chartView.titleForX = self.size;
    _chartView.titleForY = @"";
    
    [self.brokenLine addSubview:_chartView];
}

-(void)setShoesSizeArr:(NSArray *)shoesSizeArr {
    _shoesSizeArr = shoesSizeArr;
    NSArray * codeArr = [HJHomeDetailCodeModel mj_objectArrayWithKeyValuesArray:shoesSizeArr];
    [_modelsArr removeAllObjects];
    [_modelsArr addObjectsFromArray:codeArr];
    [_collectionView reloadData];
}
-(void)configCollection {
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    layout.itemSize = CGSizeMake((mScreenWidth -6*2.5)/5, (mScreenWidth -6*2.5)/5);
    
    _collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.backgroundColor = HJColorRGB(10);
    _collectionView.bounces = NO;
    _collectionView.scrollEnabled = NO;
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[HJDetailCollectionCell class] forCellWithReuseIdentifier:@"Cell"];
    [self.codeView addSubview:_collectionView];
}


-(void)layoutSubviews {
    [super layoutSubviews];
    _chartView.frame = CGRectMake(15, 0, mScreenWidth-30, self.brokenLine.height);
    _collectionView.frame = CGRectMake(0, 0, self.codeView.width, self.codeHeight.constant);
}
//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _modelsArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HJDetailCollectionCell * cell = (HJDetailCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    HJHomeDetailCodeModel * model = _modelsArr[indexPath.row];
    cell.model = model;

    return cell;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  
    return UIEdgeInsetsMake(5, 2.5, 1.25, 2.5);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0.001;
}

//点击选定
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HJDetailCollectionCell *cell = (HJDetailCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
    self.size = [NSString stringWithFormat:@"%@码",cell.codel.text];
    _chartView.titleForX = self.size;
    HJHomeDetailCodeModel * model = _modelsArr[indexPath.row];
    model.check = @1;
    cell.model = model;
    if (self.collectionWithSizeBlock) {
        self.collectionWithSizeBlock(cell.codel.text,cell.money.text);
    }
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    HJDetailCollectionCell *cell = (HJDetailCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
    HJHomeDetailCodeModel * model = _modelsArr[indexPath.row];
    model.check = @0;
    cell.model = model;
}

// 更多码子
- (IBAction)moreCodeBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
    NSInteger collectionH;
    if (sender.selected ==YES) {
        if (_modelsArr.count<5) {
            return;
        }else {
            collectionH = ((_modelsArr.count /5)+1) * ((mScreenWidth -6*2.5)/5.0+2.5)+20;
            _cellBool = NO;
        }
    }else {
        _cellBool = YES;
        collectionH = (mScreenWidth -6*2.5)/5.0 +5;
    }
    self.codeHeight.constant = collectionH;
    NSInteger cellHeight = collectionH + 280;
    [[NSNotificationCenter defaultCenter]postNotificationName:@"cellHeightNotification" object:nil userInfo:@{@"cellHeight":@(cellHeight),@"bool":@(_cellBool)}];
}

// 成交记录按钮 10 出售列表 11 求购列表 12
- (IBAction)clinchadeal:(UIButton *)sender {
    [self animationSliderWithSender:sender];
    if (self.block) {
        self.block(sender.tag);
    }
}

- (void)animationSliderWithSender:(UIButton *)btn {
    
    for (int i = 10; i<=12; i ++) {
        UIButton *view = [self viewWithTag:i];
        view.alpha = 0.5;
        view.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:(mScreenWidth/414*15)];
    }
    btn.alpha = 1;
    btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:(mScreenWidth/414*15)];
    _lineLeftW.constant = btn.center.x - _line.width / 2.0;
    
    [UIView animateWithDuration:0.1 animations:^{
        [self layoutIfNeeded];
    }];
}

@end
